﻿using Bot.Domain.EndpointAPIResponseRecords;
using Bot.Domain.SessionChat;



using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bot.Domain.Report
{
    public class UsageDetailsByIntentItem : BaseReportInfoItem
    {
        public string CategoryName { get; set; }
        public string IntentName { get; set; }
        public int Count { get; set; }

        public override string GetValue(int i)
        {
            switch (i)
            {
                case 0:
                    return this.CategoryName.ToString();
                    break;
                case 1:
                    return this.IntentName.ToString();
                    break;
                case 2:
                    return this.Count.ToString();
                    break;
                default:
                    return "";
            }
        }
    }
    public class UsageDetailsByIntentInfo : BaseReportInfo<UsageDetailsByIntentItem>
    {

        [JsonIgnore]
        public override List<string> FieldList
        {
            get
            {
                return new List<string>() {
                   
                };
            }
        }
        public override string GetValue(int i)
        {
            switch (i)
            {
                default:
                    return "";
            }
        }
    }
    public class UsageDetailsByIntentEntityInfo
    {
        public IList<GetUsageDetailsByIntentOutput> UsageDetailsByIntentEntityList { get; set; }
    }

    public class UsageDetailsByIntentWrap : BasicWrap<UsageDetailsByIntentItem, UsageDetailsByIntentInfo, UsageDetailsByIntentEntityInfo>
    {
        
        public static UsageDetailsByIntentInfo GetResponse(SearchCriteria search, UsageDetailsByIntentEntityInfo entityInfo, bool ifEncode = true)
        {
            UsageDetailsByIntentInfo result = new UsageDetailsByIntentInfo();
            //UTCToLocal(ref entityInfo);
            switch (search.dimensionType)
            {
                case EnumDimensionType.ByTime://by time
                //case EnumDimensionType.AllChannel:
                //case EnumDimensionType.DefaultChannel:
                //case EnumDimensionType.LivechatChannel:
                //case EnumDimensionType.FacebookChannel:
                //case EnumDimensionType.TwitterChannel:
                    result = GetInfoByTime(search, entityInfo, PushByTimeData, null, null);
                    break;
            }

            //watch.Stop();
            //System.Diagnostics.Debug.WriteLine("Calc Time: " + watch.Elapsed);

            return result;
        }

        //search the chat Source info for campaign by criteria
        private static UsageDetailsByIntentInfo GetResponseByDimension(SearchCriteria search, UsageDetailsByIntentEntityInfo entityInfo, bool ifEncode)
        {
            var result = new UsageDetailsByIntentInfo();
            //result.dataType = EnumReportDataType.DataByDimension;
            //List<EndpointAPIResponseRecordsOutputDto> data = entityInfo.ResponseEntityList.Where(a=>a.CodePlanId == int.Parse(search.filterStr)).ToList();

            //result.TotalResponse = data.Count;
            //if (search.dimensionType == EnumDimensionType.ByDepartment)
            //{
            //    result.TotalAgentOnly = data.Count(a=>a.ChatType== (int)EnumChatType.AgentOnly);
            //    result.TotalChatbotOnly = data.Count(a => a.ChatType == (int)EnumChatType.ChatbotOnly);
            //    result.TotalFromBotToAgent = data.Count(a => a.ChatType == (int)EnumChatType.ChatbotToAgent);
            //}

            //result.dataList = new List<ResponseItem>();

            //foreach (var item in data)
            //{
            //    var newItem = new ResponseItem();
            //    newItem.tableRowName = GetTableRowName(ifEncode, item.RequestTime);
            //    newItem.fieldId = item.FieldId;
            //    newItem.chatCount = item.Response;
            //    newItem.chatPercentage = CalcPercent(item.Response, result.totalChatCount);
            //    if (search.dimensionType == EnumDimensionType.ByDepartment)
            //    {
            //        newItem.fromChatButtonCount = item.ChatButtons;
            //        newItem.fromChatButtonPercentage = CalcPercent(item.ChatButtons, result.totalFromChatButtonCount);
            //        newItem.fromManuallyInvitationCount = item.ManuallyInvitations;
            //        newItem.fromManuallyInvitationPercentage = CalcPercent(item.ManuallyInvitations, result.totalFromManuallyInvitationCount);
            //        newItem.fromAutoInvitationCount = item.AutoInvitations;
            //        newItem.fromAutoInvitationPercentage = CalcPercent(item.AutoInvitations, result.totalFromAutoInvitationCount);
            //    }

            //    newItem.startTime = search.startDate.ToString();
            //    newItem.startTimeDate = search.startDate;
            //    newItem.endTime = search.endDate.ToString();
            //    newItem.endTimeDate = search.endDate;
            //    result.dataList.Add(newItem);
            //}

            //result.dataList = result.dataList.OrderByDescending(o => o.Time).ToList();
            return result;
        }

        private static UsageDetailsByIntentInfo PushByTimeData(UsageDetailsByIntentInfo result, UsageDetailsByIntentEntityInfo entityInfo, SearchCriteria search)
        {
            //IEnumerable<GetUsageDetailsByIntentOutput> ResponseEntityList = entityInfo.UsageDetailsByIntentEntityList.OrderBy(c => c.CallTime);
            //int lindex = 0;
            //foreach (var item in ResponseEntityList)
            //{
            //    int operatorId = 1;
            //    operatorId = SessionHelper.GetSessionValue_CurrentOperatorId();
            //    item.CallTime = DateTimeHelper.UtcToLocal(item.CallTime, operatorId); 

            //    UsageDetailsByIntentItem UsageDetailsByIntentItem = result.dataList[lindex];
            //    while (item.CallTime >= UsageDetailsByIntentItem.endTimeDate && lindex < result.dataList.Count)
            //    {
            //        lindex++;
            //        UsageDetailsByIntentItem = result.dataList[lindex];
            //    }
            //    UsageDetailsByIntentItem.Count++;
            //}
            //result.dataList = result.dataList.Reverse().ToList();
            //GetListTotal(ref result, search);
            return result;
        }

        private static void GetListTotal(ref UsageDetailsByIntentInfo result, SearchCriteria search)
        {
            double divNum = 0;
            if (search.timeUnit == EnumTimeIntervalType.Distribution24_7Hour)
            {
                divNum = GetWeeksInDateRange(search.startDate, search.endDate);
            }
            else if (search.timeUnit == EnumTimeIntervalType.Distribution48HalfHour)
            {
                divNum = GetDays(search.startDate, search.endDate);
            }           

            if (search.timeUnit == EnumTimeIntervalType.Distribution24_7Hour || search.timeUnit == EnumTimeIntervalType.Distribution48HalfHour)
            {

            }
        }

        #region export

        //search the New Export CSV Data for Chat Source Info by criteria
        public static NewExportCSVData GetExprotData(SearchCriteria search, UsageDetailsByIntentEntityInfo entityInfo)
        {
            UsageDetailsByIntentInfo info = GetResponse(search, entityInfo, false);
            NewExportCSVData result = new NewExportCSVData(string.Empty, new List<NewCSVColumn>(), new DataTable());
            switch (search.dimensionType)
            {
                case EnumDimensionType.ByTime://by time
                //case EnumDimensionType.AllChannel:
                //case EnumDimensionType.DefaultChannel:
                //case EnumDimensionType.LivechatChannel:
                //case EnumDimensionType.FacebookChannel:
                //case EnumDimensionType.TwitterChannel:
                    {
                        result = GetExprotCSVData(search, info, info.FieldList);
                        DataTable table = new DataTable();
                        table.Columns.AddRange(new DataColumn[] {
                            new DataColumn("Category"),
                            new DataColumn("Intent Name"),
                            new DataColumn("Count"),
                        });
                        table.Rows.Add(new object[] {
                            "Category",
                            "Intent Name",
                            "Count",
                        });
                        var orderList = entityInfo.UsageDetailsByIntentEntityList.OrderByDescending(a => a.Count);
                        foreach (GetUsageDetailsByIntentOutput item in orderList)
                        {
                            table.Rows.Add(new object[] {
                                item.CategoryName,
                                item.IntentName,
                                item.Count,
                            });
                        }
                        result.source = table;

                        break;
                    }
                case EnumDimensionType.RequestPage:
                    {
                        result = GetExprotCSVData(search, info, new List<string>() { });
                        break;
                    }
            }
            return result;
        }

        #endregion

    }
}
